package com.guodu.service.impl;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.guodu.dao.MaterialChaoshiDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.MaterialChaoshi;
import com.guodu.entity.MaterialchaoshiStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.MaterialChaoshiService;
import com.guodu.vo.MaterialChaoshiQueryVo;
import com.guodu.vo.MaterialChaoshiVo;

@Service("materialChaoshiService")
public class MaterialChaoshiServiceImpl implements MaterialChaoshiService {

	@Resource
	private MaterialChaoshiDao materialChaoshiDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@Override
	public List<MaterialChaoshiVo> findByVo(MaterialChaoshiQueryVo materialChaoshiQueryVo,Units units,
			int curPage, int pageSize) {
		// TODO Auto-generated method stub
		List<MaterialChaoshi> list = materialChaoshiDao.findByVo(materialChaoshiQueryVo,units, curPage, pageSize);
		List<MaterialChaoshiVo> materialChaoshiVos = new ArrayList<MaterialChaoshiVo>();
		for(MaterialChaoshi materialChaoshi : list ){
			MaterialChaoshiVo materialChaoshiVo = new MaterialChaoshiVo(materialChaoshi);
			UserInfo userInfo = userInfoDao.findById(materialChaoshi.getCreatePeople());
			Units unitsInfo = unitsDao.findById(userInfo.getUnitsId());
			materialChaoshiVo.setUnitsName(unitsInfo.getUnitsName());
			materialChaoshiVo.setIndustry(unitsInfo.getIndustry());
			materialChaoshiVo.setUnitsId(unitsInfo.getUnitsId());
			if(unitsInfo.getFunitsId()!=null){
				Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
				materialChaoshiVo.setFunitsName(funitsInfo.getUnitsName());
				materialChaoshiVo.setFunitsId(funitsInfo.getUnitsId());
			}
			materialChaoshiVo.setUserName(userInfo.getUserName());
			materialChaoshiVos.add(materialChaoshiVo);
		}
		return materialChaoshiVos;
	}

	@Override
	public int findSizeByVo(MaterialChaoshiQueryVo materialChaoshiQueryVo,Units units) {
		// TODO Auto-generated method stub
		return materialChaoshiDao.findSizeByVo(materialChaoshiQueryVo,units);
	}

	@Override
	public MaterialChaoshi findById(String id) {
		// TODO Auto-generated method stub
		return materialChaoshiDao.findById(id);
	}

	@Override
	public void del(String id) {
		// TODO Auto-generated method stub
		materialChaoshiDao.del(id);
	}

	@Override
	public void delByIds(String[] ids) {
		// TODO Auto-generated method stub
		materialChaoshiDao.delByIds(ids);
	}

	@Override
	public void save(MaterialChaoshi entity) {
		// TODO Auto-generated method stub
		materialChaoshiDao.save(entity);
	}

	@Override
	public void examine(String ids,String reason,String beforeStatus,Long afterStatus,UserInfo userInfo) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<MaterialchaoshiStatus> entities = new ArrayList<MaterialchaoshiStatus>();
		for(int i=0;i<idses.length;i++){
			MaterialchaoshiStatus entity = new MaterialchaoshiStatus();
			entity.setAfterStatus(afterStatus);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(userInfo.getUserId());
			entity.setMaterialId(idses[i]);
			entity.setReason(reason);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		materialChaoshiDao.changeStatus(entities);
		materialChaoshiDao.examine(ids,afterStatus);
	}

	@Override
	public void update(MaterialChaoshi entity) {
		// TODO Auto-generated method stub
		materialChaoshiDao.update(entity);
	}

	@Override
	public List<MaterialchaoshiStatus> findStatus(String id) {
		// TODO Auto-generated method stub
		return materialChaoshiDao.findStatus(id);
	}

	@Override
	public List<MaterialChaoshi> findAllByVo(
			MaterialChaoshiQueryVo materialChaoshiQueryVo,Units units) {
		// TODO Auto-generated method stub
		return materialChaoshiDao.findAllByVo(materialChaoshiQueryVo,units);
	}

	@Override
	public BigDecimal findAllMoney(MaterialChaoshiQueryVo materialChaoshiQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialChaoshiDao.findAllMoney(materialChaoshiQueryVo, units);
	}

	@Override
	public boolean examinAll(MaterialChaoshiQueryVo materialChaoshiQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialChaoshiDao.examinAll(materialChaoshiQueryVo,units);
	}

	@Override
	public boolean huituiAll(MaterialChaoshiQueryVo materialChaoshiQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialChaoshiDao.huituiAll(materialChaoshiQueryVo,units);
	}

	@Override
	public boolean shanchuAll(MaterialChaoshiQueryVo materialChaoshiQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialChaoshiDao.shanchuAll(materialChaoshiQueryVo,units);
	}



}
